import { defineStore } from 'pinia';
import { ref, watch } from 'vue';

export const useDarkModeStore = defineStore('darkMode', () => {
  // 夜间模式状态
  const isDarkMode = ref<boolean>(false);

  // 从 localStorage 加载夜间模式状态
  const loadDarkMode = () => {
    const saved = localStorage.getItem('darkMode');
    if (saved !== null) {
      isDarkMode.value = saved === 'true';
    }
  };

  // 切换夜间模式
  const toggleDarkMode = () => {
    isDarkMode.value = !isDarkMode.value;
    localStorage.setItem('darkMode', String(isDarkMode.value));
  };

  // 设置夜间模式
  const setDarkMode = (value: boolean) => {
    isDarkMode.value = value;
    localStorage.setItem('darkMode', String(value));
  };

  // 初始化时加载
  loadDarkMode();

  return {
    isDarkMode,
    toggleDarkMode,
    setDarkMode,
    loadDarkMode,
  };
});

